﻿using Library.Core.Domain;
using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Role
{
    public class CreateRoleSpecification : IOperateSpecification<RoleInfoEntity>
    {
        private readonly IRoleRepository _roleRepository;

        public CreateRoleSpecification(IRoleRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public async Task<bool> SatisfiedBy(RoleInfoEntity entity)
        {
            if (await _roleRepository.AnyAsync(x => x.Name.Equals(entity.Name)))
            {
                throw new DomainException("角色名称重复！");
            }
            return true;
        }
    }
}
